<template>
  <div :id="chartDOMID" style="background-color: antiquewhite;"></div>
  <chart-config v-if="chartStyleOption != null" @config-modified="onChartConfigModified"
    @delete-saved-option="console.log" @drawer-close="configDialogShow = false" :initialOptions="chartStyleOption"
    :selectionItems="selectionItems" :unchangeableItems="unchangeableItems" v-model:showDrawer="configDialogShow"
    :chartName="'chartName--default'"></chart-config>
</template>

<script setup lang="ts">
import * as echarts from "echarts"
import { ContainerLayout } from "../base/dragging-container/dragcontainers"
import { ref, defineExpose, onBeforeMount, defineProps, PropType, watch, onMounted } from "vue"
import ChartConfig from "./ChartConfig.vue"
import { ElNotification } from "element-plus";

const emits = defineEmits(["update:chart-style-option"])
const props = defineProps({
  layout: {
    type: Object as PropType<ContainerLayout>,
    required: true
  }
})
let chart: echarts.EChartsType | null = null
const chartDOMID = ref("")
const chartOption = ref<echarts.EChartsOption | null>(null)
const chartStyleOption = ref<echarts.EChartsOption | null>(null)
const configDialogShow = ref(false)
const selectionItems = {}
const unchangeableItems = {}

onBeforeMount(() => {
  chartDOMID.value = "echart-free-component-" + Math.random().toString();
})

onMounted(async () => {
  if (chart != null) {
    console.error("Chart was already initialized!")
    return
  }
  chart = echarts.init(
    document.getElementById(chartDOMID.value) as any
  );
})

function onChartConfigModified(config: any): void {
  chartOption.value = config;
  console.log(chartOption.value)
  if (chartOption.value != null) {
    setStyleOption(chartOption.value as echarts.EChartsOption);
    emits("update:chart-style-option", chartStyleOption.value)
  } else {
    console.error("chart option was null")
  }
}

function setStyleOption(options: echarts.EChartsOption) {
  if (chart == null) {
    console.error("Chart was null!")
    return
  }
  chartStyleOption.value = options
  chart.setOption(options)
}

function setOption(options: echarts.EChartsOption, clear?: boolean) {
  if (clear === undefined) {
    clear = true
  }
  chartOption.value = options
  if (chart == null) {
    console.error("Chart was null!")
    return
  }
  if (clear) {
    chart.clear()
  }
  chart.setOption(options)
  chart.setOption({
    toolbox: {
      show: true,
      feature: {
        dataView: { readOnly: false },
        dataZoom: {},
        saveAsImage: {},
        myTool1: {
          show: true,
          title: "Chart Configure",
          icon: "M8 4.754a3.246 3.246 0 1 0 0 6.492 3.246 3.246 0 0 0 0-6.492zM5.754 8a2.246 2.246 0 1 1 4.492 0 2.246 2.246 0 0 1-4.492 0z M9.796 1.343c-.527-1.79-3.065-1.79-3.592 0l-.094.319a.873.873 0 0 1-1.255.52l-.292-.16c-1.64-.892-3.433.902-2.54 2.541l.159.292a.873.873 0 0 1-.52 1.255l-.319.094c-1.79.527-1.79 3.065 0 3.592l.319.094a.873.873 0 0 1 .52 1.255l-.16.292c-.892 1.64.901 3.434 2.541 2.54l.292-.159a.873.873 0 0 1 1.255.52l.094.319c.527 1.79 3.065 1.79 3.592 0l.094-.319a.873.873 0 0 1 1.255-.52l.292.16c1.64.893 3.434-.902 2.54-2.541l-.159-.292a.873.873 0 0 1 .52-1.255l.319-.094c1.79-.527 1.79-3.065 0-3.592l-.319-.094a.873.873 0 0 1-.52-1.255l.16-.292c.893-1.64-.902-3.433-2.541-2.54l-.292.159a.873.873 0 0 1-1.255-.52l-.094-.319zm-2.633.283c.246-.835 1.428-.835 1.674 0l.094.319a1.873 1.873 0 0 0 2.693 1.115l.291-.16c.764-.415 1.6.42 1.184 1.185l-.159.292a1.873 1.873 0 0 0 1.116 2.692l.318.094c.835.246.835 1.428 0 1.674l-.319.094a1.873 1.873 0 0 0-1.115 2.693l.16.291c.415.764-.42 1.6-1.185 1.184l-.291-.159a1.873 1.873 0 0 0-2.693 1.116l-.094.318c-.246.835-1.428.835-1.674 0l-.094-.319a1.873 1.873 0 0 0-2.692-1.115l-.292.16c-.764.415-1.6-.42-1.184-1.185l.159-.291A1.873 1.873 0 0 0 1.945 8.93l-.319-.094c-.835-.246-.835-1.428 0-1.674l.319-.094A1.873 1.873 0 0 0 3.06 4.377l-.16-.292c-.415-.764.42-1.6 1.185-1.184l.292.159a1.873 1.873 0 0 0 2.692-1.115l.094-.319z",
          onclick: () => {
            configDialogShow.value = true;
            if (chartStyleOption.value == null) {
              ElNotification.error({
                title: "Error",
                message: "No chart style option was set!",
              });
            }
          },
        },
      }
    }
  })
}
// 设置Div元素的位置及长宽
const setDivLayout = (layout: ContainerLayout) => {
  const elem = document.getElementById(chartDOMID.value)
  if (elem != null) {
    elem.style.width = layout.width.toString()
    elem.style.height = layout.height.toString()
    elem.style.top = layout.top.toString()
    elem.style.left = layout.left.toString()
  } else {
    console.error("base div element cannot get")
  }
}

const updateLayout = () => {
  if (chart != null) {
    setDivLayout(props.layout)
    chart.resize({ width: props.layout.width, height: props.layout.height });
  }
}

watch(() => props.layout, updateLayout)

defineExpose({ setOption, setStyleOption })
</script>